<?php
App::uses('AppController', 'Controller');
/**
 * PlanosTags Controller
 *
 * @property PlanosTag $PlanosTag
 */
class PlanosTagsController extends AppController {

    public $layout = 'epcm';

	public function index() {
		$this->PlanosTag->recursive = 0;
		$this->set('planosTags', $this->paginate());
	}
    
    public function insertar($idPlano = null)
    {
        $this->set(compact('idPlano'));
        //$this
        
        if(!empty($this->data))
        {
            if($this->PlanosTag->saveMany($this->request->data))
            {
                $this->Session->setFlash('Guardado correctamente');
                $this->redirect(array('action'=>'listado', $idPlano));
            }
        }
    }
    
    public function listado($idPlano = null){
        
        $tags = $this->PlanosTag->find('all', array(
            'recursive'=>0,
            'conditions'=>array('PlanosTag.plano_id'=>$idPlano)
        )); 
        $this->set(compact('tags'));
    }
    
    public function editar($id = null)
    {
        $this->PlanosTag->id = $id;
        $plano = $this->PlanosTag->find('first', array(
           'recursive'=>-1,
            'conditions'=>array('id'=>$id)
        ));
        $idPlano = $plano['PlanosTag']['plano_id'];
        
        if (!$id) {
            $this->Session->setFlash('No Existe el tipo de Plano');
            $this->redirect(array('action' => 'index'));
        }
        if (empty($this->data)) {
            $this->data = $this->PlanosTag->read();
        } else {
            if ($this->PlanosTag->save($this->data)) {
                $this->Session->setFlash('Se Guardo Correctamente el Plano');
                $this->redirect(array('action' => 'listado', $idPlano));
            } else {
                $this->Session->setFlash('Error al guardar el Plano');
            }
        }
        $this->set(compact('plano'));
    }        
/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->PlanosTag->id = $id;
		if (!$this->PlanosTag->exists()) {
			throw new NotFoundException(__('Invalid planos tag'));
		}
		$this->set('planosTag', $this->PlanosTag->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->PlanosTag->create();
			if ($this->PlanosTag->save($this->request->data)) {
				$this->Session->setFlash(__('The planos tag has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The planos tag could not be saved. Please, try again.'));
			}
		}
		$planos = $this->PlanosTag->Plano->find('list');
		$tags = $this->PlanosTag->Tag->find('list');
		$this->set(compact('planos', 'tags'));
	}

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->PlanosTag->id = $id;
		if (!$this->PlanosTag->exists()) {
			throw new NotFoundException(__('Invalid planos tag'));
		}
		if ($this->PlanosTag->delete()) {
			$this->Session->setFlash(__('Planos tag deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Planos tag was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
    
    public function eliminar($idPlanosTag = null)
    {
         
        $celimina=$this->PlanosTag->find('first', array('recursive'=>-1,
        'conditions'=>array('id'=>$idPlanosTag)
        ));
        
        // debug($celimina);exit;
        $cambio=$celimina['PlanosTag']['plano_id'];
         //debug($cambio);
        
        $this->PlanosTag->id = $idPlanosTag;
        $this->data = $this->PlanosTag->read();
        if (!$idPlanosTag)
        {
            $this->Session->setFlash('No existe el Plano Tags a Eliminar');
            $this->redirect(array('action' => 'index'));
        } else
        {
            if ($this->PlanosTag->delete($idPlanosTag))
            {
                $this->Session->setFlash('Se elimino Correctamente');
                $this->redirect(array('controller' => 'planostags', 'action' => 'listado', $cambio));
            } else
            {
                $this->Session->setFlash('Error al eliminar');
            }
        }
    }
}
